package com.tdk.wifidirectserver.ui.main

import android.content.Context
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import com.tdk.wifidirectserver.APConnectActivity
import com.tdk.wifidirectserver.MainActivity
import com.tdk.wifidirectserver.R
import com.tdk.wifidirectserver.databinding.FragmentConnectedServerBinding
import com.tdk.wifidirectserver.ui.chat.ChatFragment


/**
 * A simple [Fragment] subclass.
 * Use the [ConnectedFragment.newInstance] factory method to
 * create an instance of this fragment.
 */
class ConnectedServerFragment : Fragment() {

    lateinit var binding: FragmentConnectedServerBinding
    lateinit var viewModel: MainViewModel
    lateinit var connectedServerViewModel: ConnectedServerViewModel


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        connectedServerViewModel = ViewModelProvider(this)[ConnectedServerViewModel::class.java]
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = FragmentConnectedServerBinding.inflate(layoutInflater, container, false)
        initView()
        return binding.root
    }

    private fun initView() {

        binding.identifyInfoTv.text = "GroupOwner"
        connectedServerViewModel.serverCreateStatus.observe(viewLifecycleOwner) {
            Log.d(TAG, "socketConnectStatus: $it")
            if (it) {
                binding.btCloseServer.isEnabled = true
                binding.btStartServer.isEnabled = false
                activity?.supportFragmentManager?.beginTransaction()
                    ?.replace(
                        R.id.fragment_container,
                        ChatFragment.newInstance(true, "", connectedServerViewModel),
                        ChatFragment::class.java.simpleName
                    )
                    ?.commit()

            } else {
                binding.btStartServer.isEnabled = true
                binding.btCloseServer.isEnabled = false
                val chatFragment =
                    activity?.supportFragmentManager?.findFragmentByTag(ChatFragment::class.java.simpleName)
                if (chatFragment != null) {
                    activity?.supportFragmentManager?.beginTransaction()?.remove(chatFragment)?.commit()
                }
            }

        }
        if (context is APConnectActivity) {
            binding.btDisconnect.visibility = View.GONE
        }
        binding.btDisconnect.setOnClickListener {
            viewModel.disconnect()
        }

        binding.btStartServer.setOnClickListener {
            connectedServerViewModel.startServer()
        }

        binding.btCloseServer.setOnClickListener {
            connectedServerViewModel.closeServer()
        }


        binding.btStartNsd.setOnClickListener {
            connectedServerViewModel.startNsd()
        }

        binding.btStopNsd.setOnClickListener {
            connectedServerViewModel.stopNsd()
        }

    }

    companion object {
        @JvmStatic
        fun newInstance() =
            ConnectedServerFragment()

        private const val TAG = "ConnectedServerFragment"
    }

    override fun onAttach(context: Context) {
        super.onAttach(context)
        if (context is MainActivity) {
            viewModel = context.viewModel
        }


    }
}